<?php

namespace App\Http\Controllers\Manage;

use App\Model\ClientCase;
use App\Model\Industry;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Model\Client;
use App\Http\Controllers\Dingtalk\DingtalkController as Dt;
use Illuminate\Support\Facades\DB;
use Session;
use App\Model\Region;

class ClientController extends Controller
{

    // 所在地
    public function location()
    {
        $location = null;
        $region = Region::select('name')->where('level', '1')->get()->toArray();
        foreach ($region as $k => $v) {
            $location[] = $v['name'];
        }
        return $location;
    }

    protected static $field = [
        ['id'=>1,'searchtype'=>'编号'],
        ['id'=>2,'searchtype'=>'创建时间'],
        ['id'=>3,'searchtype'=>'公司名称'],
        ['id'=>4,'searchtype'=>'所属行业'],
        ['id'=>5,'searchtype'=>'第一联系人'],
        ['id'=>6,'searchtype'=>'第一联系人电话'],
        ['id'=>7,'searchtype'=>'第二联系人'],
        ['id'=>8,'searchtype'=>'第二联系人电话'],
        ['id'=>9,'searchtype'=>'所在地'],
        ['id'=>10,'searchtype'=>'公司地址'],
        ['id'=>11,'searchtype'=>'添加人'],
    ];

    //
    /**
     * 客户列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function clientList() {
        $user = session('user');
        $location = $this->location();
        $search = Input::get('type');
        $param = Input::get('param');
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
//        dump(input::all());
        $obj = new Client;
        $id = array();
        $dd_id = DB::table("admin")->where("username",$user["name"])->value("dd_id");
        $root = $this->getDeparUser($dd_id);
        foreach ($root as $value){
            $department[] = array('department_id' => $value["department_id"],'department' => $value["department"]);
            foreach ($value["dd_ids"] as $val){
                if($value["is_leader"]){
                    $person[] =  $val;//负责人
                }
            }
        }
        if(!empty($person)){
            $obj = $obj->whereIn("dd_id",$person);
        }else{
            $obj = $obj->where("dd_id",$dd_id);
        }
        if($search == 1){
            $obj = $obj->where('id','like',"%$param%");
        }elseif ($search == 2){
            $obj = $obj->where('create_time','like',"%$param%");
        }elseif ($search == 3){
            $obj = $obj->where('company','like',"%$param%");
        }elseif ($search == 4){
            $industry = Industry::where('name','like',"%$param%")->get();
            foreach ($industry as $i) {
                $id[] = $i['id'];
            }
            $obj = $obj->whereIn('industry_id',$id);
        }elseif ($search == 5){
            $obj = $obj->where('first_name','like',"%$param%");
        }elseif ($search == 6){
            $obj = $obj->where('first_phone','like',"%$param%");
        }elseif ($search == 7){
            $obj = $obj->where('second_name','like',"%$param%");
        }elseif ($search == 8){
            $obj = $obj->where('second_phone','like',"%$param%");
        }elseif ($search == 9){
            $obj = $obj->where('location','like',"%$param%");
        }elseif ($search == 10){
            $obj = $obj->where('address','like',"%$param%");
        }elseif ($search == 11){
            $obj = $obj->where('dd_id','like',"%$param%");
        }
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $client = $obj->Paginate(10);
        if(empty($page)){
            $count = $obj->count();
        }
        if($client == false && !empty($search)){
            return redirect()->back()->withSuccess('未搜索到结果！');
        }
        //获取图片
        $picture = array();
        foreach ($client as $k=>$v){
            if(!empty($v->picture)) {
                $picture[$k] = explode(',',$v->picture);
            }
            else {
                $picture[$k] = 0;
            }
        }
        //获取所在地
        foreach ($location as $l => $value) {
            foreach ($client as $r => $res) {
                if($l == $res['location']) {
                    $client[$r]['loc'] = $value;
                }
            }
        }
        $searchtype = self::$field;

        // 获取ddid对应的用户
        if(Session::has('users')) {
            $users = Session::get('users');
        }
        else {
            $dt = new Dt;
            $users = $dt->getUsers();
            Session::put('users', $users);
        }
/*        Session::forget('users');
        $dt = new Dt;
        $users = $dt->getUsers();
        dump($users);
        exit;*/
        return view('manage.client.clientList',[
            'client'=>$client,
            'picture'=>$picture,
            'searchtype'=>$searchtype,
            'search'=>$search,
            'param'=>$param,
            'orderBy'=>$orderBy,
            'count'=>$count,
            'active1' => 2,
            'users' => $users
        ]);
    }

    /**
     * 客户状态管理
     * @return array
     */
    public function clientStatus() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 0) {
            $map = Client::where('id',$id)->update(['status'=>0]);
            $code = 100;
            $msg = '设置成功！';
        }elseif($status == 1){
            $map = Client::where('id',$id)->update(['status'=>1]);
            $code = 100;
            $msg = '设置成功！';
        }elseif($status == 2){
            $map = Client::where('id',$id)->update(['status'=>2]);
            $code = 100;
            $msg = '设置成功！';
        }else {
            $map = null;
            $code = 102;
            $msg = '设置失败！';
        }
        if(!$map){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>$code,
            'msg'=>$msg
        ];
    }

    /*
     * 更改添加人
     * @return array
     */
    public function clientddid_select() {
        $id = Input::get('id');
        $user_id = Input::get('user_id');
        $map = Client::where('id',$id)->update(['dd_id'=>$user_id]);
        if($map) {
            $code = 100;
            $msg = '设置成功！';
            $dt = new Dt;
            $users = $dt->getUsers();
            Session::put('users', $users);
        }
        else {
            $code = 101;
            $msg = '设置失败！';
        }
        return [
            'code'=>$code,
            'msg'=>$msg
        ];
    }

    public function client_detail($id) {
        $client = Client::getClientInfo($id);
        $location = $this->location();
        //获取所在地
        foreach ($location as $l => $value) {
            if($l == $client->location){
                $client['loc'] = $value;
            }
        }
        // 获取ddid对应的用户
        if(Session::has('users')) {
            $users = Session::get('users');
        }
        else {
            $dt = new Dt;
            $users = $dt->getUsers();
            Session::put('users', $users);
        }
        $pic = explode(',',$client->picture);
        return view('manage.client.client_detail',['client'=>$client,'pic'=>$pic,'active1' => 2,'users'=>$users]);
    }
}
